﻿using AjaxControlToolkit;
using Marbles.CMS.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using AjaxControlToolkit.HTMLEditor;
using System.Data;
using Marbles.CMS.ContentTypes;

namespace Marbles.CMS.ContentTypes.Article
{
    public partial class Editor : ContentEditorBase, IContentEditor
    {
        private TemplateDefinition CurrentDefinition = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "Edit Article";

            if (!Page.IsPostBack)
            {
                InitialiseForm();
                LoadContent();
            }
        }

        private void InitialiseForm()
        {
            ContentManager cntlr = new ContentManager();
            Dictionary<string, string> items = cntlr.GetAvailableTemplates();
            Dictionary<string, string> pages = cntlr.GetAllPages();
            drpTemplate.ClearSelection();
            drpTemplate.Items.Clear();
            drpTemplate.Items.Add(new ListItem(string.Empty, string.Empty));

            drpType.Items.Clear();
            drpType.ClearSelection();

            drpType.Items.Add(new ListItem(string.Empty, string.Empty));

            foreach (PageRenderer pg in cntlr.GetInstalledRenderers())
            {
                drpType.Items.Add(new ListItem(pg.Title, pg.PageType));
            }

            foreach (string s in items.Keys)
            {
                drpTemplate.Items.Add(new ListItem(items[s], s));
            }

            drpParent.ClearSelection();
            drpParent.Items.Clear();
            drpParent.Items.Add(new ListItem(string.Empty, string.Empty));

            foreach (string s in pages.Keys)
            {
                drpParent.Items.Add(new ListItem(pages[s], s));
            }
        }

        private void LoadContent()
        {

            if (string.IsNullOrEmpty(ContentID))
            {
                this.Title = "Create Article";
                btnSave.CommandName = "Insert";
                btnSave.CommandArgument = string.Empty;
                txtName.Focus();
                drpType.Enabled = false;
                valType.Enabled = false;
                drpType.SelectedValue = "article";
            }
            else
            {
                drpType.Enabled = false;
                valType.Enabled = false;
                btnSave.CommandName = "Update";
                btnSave.CommandArgument = ContentID;
                ContentManager cntlr = new ContentManager();
                PageDefinition def = cntlr.GetPageDefinition(ContentID);


                if (def != null)
                {
                    this.CurrentDefinition = cntlr.GetTemplateDefinition(def.TemplateName);
                    txtName.ReadOnly = true;
                    txtName.Enabled = false;
                    drpType.SelectedValue = def.Renderer.PageType;
                    drpParent.SelectedValue = def.ParentPageName;
                    drpTemplate.Focus();
                    chkVisible.Checked = def.Visible;
                    chkAnonymous.Checked = def.Anonymous;
                    txtDescription.Text = def.Description;
                    txtKeywords.Text = def.Keywords;
                    txtName.Text = def.PageName;
                    txtTitle.Text = def.Title;
                    drpTemplate.SelectedValue = def.TemplateName;
                    Dictionary<string, string> pageContent = null;
                    pageContent = cntlr.GetPageContent(def.PageName);
                    lstContent.DataSource = pageContent;
                    lstContent.DataBind();
                    lstContent.Visible = true;
                    ResizeForm();
                }
            }
        }

        protected void drpTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            ContentManager cntlr = new ContentManager();
            this.CurrentDefinition = cntlr.GetTemplateDefinition(drpTemplate.SelectedValue);

            if (this.CurrentDefinition != null)
            {
                lstContent.DataSource = this.CurrentDefinition.GetDictionary();
                lstContent.DataBind();
                lstContent.Visible = true;
                ResizeForm();
            }
        }

        private void ResizeForm()
        {
            string key = string.Empty;

            for (int i = 0; i < lstContent.Items.Count; i++)
            {
                AjaxControlToolkit.HTMLEditor.Editor txtContent = lstContent.Items[i].FindControl("txtContent") as AjaxControlToolkit.HTMLEditor.Editor;
                key = lstContent.DataKeys[i].ToString();
                txtContent.Height = new Unit(this.CurrentDefinition.ContentIds[i].EditorHeight, UnitType.Pixel);
            }

            pnlEditorScroll.ScrollBars = ScrollBars.Both;

            if (lstContent.Items.Count <= 1)
            {
                pnlEditorScroll.ScrollBars = ScrollBars.None;
            }
        }

        protected void btnSave_Command(object sender, CommandEventArgs e)
        {
            ContentManager cntlr = new ContentManager();
            Dictionary<string, string> Content = new Dictionary<string, string>();
            PageDefinition def = null;

            for (int i = 0; i < lstContent.DataKeys.Count; i++)
            {
                AjaxControlToolkit.HTMLEditor.Editor txtContent = lstContent.Items[i].FindControl("txtContent") as AjaxControlToolkit.HTMLEditor.Editor;
                Content.Add(lstContent.DataKeys[i].ToString(), txtContent.Content);
            }

            switch (e.CommandName)
            {
                case "Insert":
                    def = new PageDefinition();
                    def.Renderer = cntlr.GetPageRendererFromType(drpType.SelectedValue);
                    def.PageName = this.txtName.Text;
                    if (def.PageName.Substring(0, 1) != "/") { def.PageName = "/" + def.PageName; }
                    def.Description = this.txtDescription.Text;
                    def.Keywords = this.txtKeywords.Text;
                    def.Title = this.txtTitle.Text;
                    def.TemplateName = drpTemplate.SelectedValue;
                    def.ParentPageName = drpParent.SelectedValue;
                    def.Visible = chkVisible.Checked;
                    def.Anonymous = chkAnonymous.Checked;
                    cntlr.InsertContent(def, Content);
                    break;
                case "Update":
                    def = new PageDefinition();
                    def.Renderer = cntlr.GetPageRendererFromType(drpType.SelectedValue);
                    def = cntlr.GetPageDefinition(e.CommandArgument.ToString());
                    def.PageName = this.txtName.Text;
                    def.Description = this.txtDescription.Text;
                    def.Keywords = this.txtKeywords.Text;
                    def.Title = this.txtTitle.Text;
                    def.TemplateName = drpTemplate.SelectedValue;
                    def.ParentPageName = drpParent.SelectedValue;
                    def.Visible = chkVisible.Checked;
                    def.Anonymous = chkAnonymous.Checked;
                    cntlr.UpdateContent(def, Content);
                    break;
                case "Delete":
                    cntlr.DeleteContent(e.CommandArgument.ToString());
                    break;
            }

            cntlr.GenerateSiteMap();
            Response.Redirect("Default.aspx?action=ArticleList", true);
        }

        protected void lstContent_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label lblKey = e.Item.FindControl("lblKey") as Label;
            AjaxControlToolkit.HTMLEditor.Editor txtContent = e.Item.FindControl("txtContent") as AjaxControlToolkit.HTMLEditor.Editor;
            txtContent.Content = string.Empty;

            KeyValuePair<string, string> item = (KeyValuePair<string, string>)e.Item.DataItem;
            txtContent.Content = item.Value;
        }


    }
}